package com.bii.gelapp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingSaveDialog extends Dialog {

	public Context savecontext;
	Button btSave, btCancel;
	EditText etSave;

	public SettingSaveDialog(Context context) {
		super(context);
		try {
			this.savecontext = context;
		} catch (Exception ex) {

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.settingsavedialog);

		WindowManager.LayoutParams wmlp = this.getWindow().getAttributes();
		WindowManager wm = (WindowManager) this.savecontext
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		wmlp.gravity = Gravity.CENTER_VERTICAL;
		wmlp.width = display.getWidth();

		btCancel = (Button) findViewById(R.id.btCancel);
		etSave = (EditText) findViewById(R.id.etSave);
		String filename = "settings 1";
		etSave.setText(filename);

		btSave = (Button) findViewById(R.id.btSave);
		btSave.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String sttitle = etSave.getText().toString();
				// to be stored:
				Settings settings = new Settings();
				settings.title = sttitle;
				ArrayList Settingslist = new ArrayList();
				
				int brightnessprogress = BrightnessActivity2.currentprogress;
				int gaussianprogress = GaussianActivity.currentprogress;
				int thresholdprogress = GaborActivity2.thresholdvalue;
				int sigma = GaborActivity2.sigmavalue;
				int lambda = GaborActivity2.lambdavalue;
				int segmentationminsize = SegmentationActivity.minsize;
				int inverse = GaborActivity2.inverse;
				
				settings.brightnessprogress =brightnessprogress;
				settings.gaussianprogress = gaussianprogress;
				settings.thresholdprogress =  thresholdprogress;
				settings.segmentationminsize = segmentationminsize;
				settings.sigma = sigma;
				settings.lambda = lambda;
				//18/8
				settings.inverse = inverse;
				
				//settings.brightnessprogress = BrightnessActivity2.currentprogress;
				//settings.gaussianprogress = GaussianActivity.currentprogress;
				//settings.thresholdprogress = GaborActivity.currentprogress;
				//settings.segmentationminsize = SegmentationActivity.minsize;

				if (fileExistance("listofsettings") == true) {
					try {
						if (fileExistance(sttitle) == true) {
							Settingslist = (ArrayList) InternalStorage.readObject(
									savecontext, "listofsettings");
							InternalStorage.writeObject(savecontext,
									"listofsettings", Settingslist);
							InternalStorage.writeObject(savecontext, sttitle,
									settings);
						}
						else
						{
							Settingslist = (ArrayList) InternalStorage.readObject(
									savecontext, "listofsettings");
							Settingslist.add(sttitle);
							InternalStorage.writeObject(savecontext,
									"listofsettings", Settingslist);
							InternalStorage.writeObject(savecontext, sttitle,
									settings);
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} else {

					try {
						Settingslist.add(sttitle);
						InternalStorage.writeObject(savecontext,
								"listofsettings", Settingslist);
						InternalStorage.writeObject(savecontext, sttitle,
								settings);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				Toast.makeText(savecontext, "Settings Saved", Toast.LENGTH_SHORT);
				dismiss();
			}

		});

	}

	public boolean fileExistance(String key) {
		File file = savecontext.getFileStreamPath(key);
		return file.exists();
	}
}
